package cn.mxb.logic.action;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.MxbCard;
import cn.mxb.logic.entity.MxbPolicy;
import cn.mxb.logic.entity.MxbPrsinfo;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysUser;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.entity.SysZu;
import cn.mxb.logic.entity.SysFamily;
import cn.mxb.logic.service.IMxbCardService;
import cn.mxb.logic.service.IMxbPolicyService;
import cn.mxb.logic.service.IMxbPrsinfoService;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysUserService;
import cn.mxb.logic.service.ISysVillageService;
import cn.mxb.logic.service.ISysZuService;
import unit.IdGenerator;
import unit.MyTime;
import cn.mxb.logic.service.ISysFamilyService;

@SuppressWarnings({ "serial", "rawtypes" })
public class SysFamilyAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/family/familyList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/family/familyAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/family/familyUpdate.jsp";
	protected static final String FAMILYHOMELIST_JSP = "/WEB-INF/page/web/family/familyHomeList.jsp";
	protected static final String FEMILYPEOPLEADD = "/WEB-INF/page/web/family/familypeopleAdd.jsp";
	//protected static final String PRSINFOLISTJSP = "/WEB-INF/page/web/prsinfo/prsinfoList.jsp";
	
	private SysFamily sysFamily;
	@Autowired
	private ISysFamilyService sysFamilyService;
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	@Autowired
	private ISysZuService sysZuService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IMxbPrsinfoService mxbPrsinfoService;
	@Autowired
	private IMxbCardService mxbCardService;
	@Autowired
	private IMxbPolicyService mxbPolicyService;
	
	private Result<SysFamily> pageResult;
	
	private List<SysFamily> sysFamilyAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	private List<SysZu> sysZuAllList; 
	
	private String  Idcard; //页面-身份证号
	private String  sexY; //页面-性别
	private String  addpeoplename;//页面-名字
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();  //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();  //镇名
	private Map<String,String> villageNameMap = new HashMap<String, String>();  //村名
	private Map<String,String> zuNameMap = new HashMap<String, String>();
	private Map<String,String> huzhuNameMap = new HashMap<String, String>(); 
	private Map<String,String> FamilyPeopleMap = new HashMap<String, String>(); 
	private Map<String,String> idCardMap = new HashMap<String, String>(); 
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		sysZuAllList = sysZuService.findAllList();
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 参合登记
	 * @return
	 * @throws Exception 
	 */
	public String ginseng() throws Exception{
		sysFamily = sysFamilyService.findByProperty("id",sysFamily.getId()+"");
		MxbPrsinfo mxbPrsinfo = new MxbPrsinfo();
		mxbPrsinfo.setJoinYear(MyTime.getNowDateTwo());
		IdGenerator id = new IdGenerator(1023);
		mxbPrsinfo.setInvNumber(id.getNextId()+"");
		//根据参合人员的家庭情况设置之后参合人员的参合id
		if(null != sysFamily.getFamilyId()  && !"".equals(sysFamily.getFamilyId())){
		    sysFamilyAllList = sysFamilyService.findListByPropertyTwo("familyId",sysFamily.getFamilyId(),"prsinfoState","1");
		    int canhe = sysFamilyAllList.size();
			DecimalFormat decimalFormat=new DecimalFormat("00");
			String str = decimalFormat.format(++canhe);
			mxbPrsinfo.setPrsinfoNumber(sysFamily.getFamilyId()+str);
		}
		mxbPrsinfo.setPrsinfoId(sysFamily.getFamilyPeopleId());
		//报销金额-机构id后面待改
		//直接以当前年份时间参合
		MxbPolicy mxbPolicy = new MxbPolicy();
		mxbPolicy = mxbPolicyService.findByProperty("policyDate", MyTime.getStringNowYear());
		mxbPrsinfo.setBxAllMoney(mxbPolicy.getPolicyMaxLine());
		mxbPrsinfo.setBxBalance(0.0);
		mxbPrsinfo.setHandleId("667022793");
		mxbPrsinfo.setPrsinfoState(1);
		mxbPrsinfo.setPolicyId(mxbPolicy.getPolicyId());
		mxbPrsinfoService.add(mxbPrsinfo);
		//添加完参合人员信息后，修改参合人员家庭表的参合状态为参合（1）
		sysFamilyService.updatetablebyproperty("SysFamily","prsinfoState", "familyPeopleId",sysFamily.getFamilyPeopleId(),1);
		//参合完成后，给予一本慢性病参合证
		MxbCard mxbCard = new MxbCard();
		mxbCard.setPersNumber(mxbPrsinfo.getPrsinfoNumber());
		mxbCard.setUserId(mxbPrsinfo.getPrsinfoId());
		mxbCard.setMxbCardId(id.getNextId()+"");
		mxbCardService.add(mxbCard);
		return openList();
		
	}
	
	/**
	 * 添加家庭成员
	 * @return
	 */
	public String familyPeopleAdd(){
    	SysUser sysUser = new SysUser();
    	if(null != sysFamily.getFamilyId()  && !"".equals(sysFamily.getFamilyId())){
			//查询该组家庭人数
			int number = sysFamilyService.findListByProperty("familyId",sysFamily.getFamilyId()).size();
			DecimalFormat decimalFormat=new DecimalFormat("0000");
			String str = decimalFormat.format(++number);
			sysUser.setUserId(sysFamily.getFamilyId()+str);
		}
    	//将新增成员信息add到家庭表
    	sysFamily.setFamilyPeopleId(sysUser.getUserId());
    	sysFamily.setPrsinfoState(0);
    	sysFamilyService.add(sysFamily);
    	//将成员信息add到用户表
    	sysUser.setUserRealName(addpeoplename);
    	sysUser.setUserName(addpeoplename);
		sysUser.setRoleId("crowd");
		sysUser.setCardId(Idcard);
		sysUser.setUserSex(Integer.valueOf(sexY));
		sysUser.setFamilyId(sysFamily.getFamilyId());
		sysUserService.add(sysUser);
		setForwardView(FAMILYHOMELIST_JSP);
		return SUCCESS;
		
	}
	/**
	 * 打开添加家庭成员界面
	 */
    public String openAddFamilyPeolle(){
    	System.out.println("familyId"+sysFamily.getFamilyId());
    	sysFamily = sysFamilyService.findByProperty("familyId",sysFamily.getFamilyId()+"");
    	if(null !=sysFamily.getFamilyHeadId() && !"".equals(sysFamily.getFamilyHeadId())){
    		huzhuNameMap.put(sysFamily.getFamilyHeadId(), sysUserService.findByProperty("userId", sysFamily.getFamilyHeadId()).getUserRealName());
    	}
    	setForwardView(FEMILYPEOPLEADD);
    	return SUCCESS;
    }
	
	/**
	 * 添加
	 * @throws Exception 
	 * @return_type: String
	 */
	public String add() throws Exception{
		System.out.println("add");
		/*IdGenerator id = new IdGenerator(1023);
		sysFamily.setFamilyId((id.getNextId()).intValue());*/
		
		return openList();		
	}
	/**
	 * 家庭档案创建
	 * @return
	 */
	public String familyAdd(){
		try{
			
			SysUser sysUser = new SysUser();
			if(sysFamily.getZuId() !=null && !"".equals(sysFamily.getZuId())){
				//查询该组家庭数量
				int number = sysFamilyService.findListByProperty("zuId",sysFamily.getZuId()).size();
				DecimalFormat decimalFormat=new DecimalFormat("0000");
				String str = decimalFormat.format(++number);
				sysFamily.setFamilyId(sysFamily.getZuId()+str);
				sysFamily.setRelation(5);
			}
			if(sysFamily.getFamilyId() !=null && !"".equals(sysFamily.getFamilyId())){
				//查询该组家庭人数
				int number = sysFamilyService.findListByProperty("familyId",sysFamily.getFamilyId()).size();
				DecimalFormat decimalFormat=new DecimalFormat("0000");
				String str = decimalFormat.format(++number);
				sysUser.setUserId(sysFamily.getFamilyId()+str);
			}
			sysFamily.setFamilyHeadId(sysUser.getUserId());
			sysFamily.setFamilyPeopleId(sysUser.getUserId());
			sysFamily.setFamilyAddress(sysFamily.getFamilyAddress()+sysFamily.getFamilyId());
			sysFamily.setPrsinfoState(0);
			sysFamilyService.add(sysFamily);
			//将户主信息添加到用户表
			sysUser.setUserName(sysFamily.getFamilyName());
			sysUser.setRoleId("crowd");
			sysUser.setCardId(Idcard);
			sysUser.setUserRealName(sysFamily.getFamilyName());
			sysUser.setUserSex(Integer.valueOf(sexY));
			sysUser.setFamilyId(sysFamily.getFamilyId());
			sysUserService.add(sysUser);
			sysFamily = null;
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		setForwardView(List_JSP);
		return SUCCESS;
		
	}
	/**
	 * 查询家庭成员信息
	 * @return
	 */
	public String queryFamilypeople(){
		sysFamilyAllList = sysFamilyService.findListByProperty("familyId",sysFamily.getFamilyId());
		setForwardView(FAMILYHOMELIST_JSP);
		return SUCCESS;
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			 if((null != sysFamily.getFamilyName() && !"".equals(sysFamily.getFamilyName())) || (null != sysFamily.getFamilyId() && !"".equals(sysFamily.getFamilyId()))){
				 setForwardView(FAMILYHOMELIST_JSP);
			 }else{
				 setForwardView(List_JSP);
			 }
			pageResult = sysFamilyService.find(sysFamily, getPage(), getRow());
			for(SysFamily sysFamily : pageResult.getData()){
				if(null != sysFamily.getCountId()  && !"".equals(sysFamily.getCountId())){
					countNameMap.put(sysFamily.getCountId(),sysCountService.findByProperty("countId", sysFamily.getCountId()+"").getCountName());
				}
				if(null != sysFamily.getTownId() && !"".equals(sysFamily.getTownId())){
					townNameMap.put(sysFamily.getTownId(),sysTownService.findByProperty("sysTownId", sysFamily.getTownId()+"").getSysTownName());
				}
				if(null != sysFamily.getVillageId() && !"".equals(sysFamily.getVillageId())){
					villageNameMap.put(sysFamily.getVillageId(),sysVillageService.findByProperty("villageId", sysFamily.getVillageId()+"").getVillageName());
				}
				if(null != sysFamily.getZuId() && !"".equals(sysFamily.getZuId())){
					zuNameMap.put(sysFamily.getZuId(),sysZuService.findByProperty("zuId", sysFamily.getZuId()).getZuName());
				}
				if(null != sysFamily.getFamilyHeadId() && !"".equals(sysFamily.getFamilyHeadId())){
					huzhuNameMap.put(sysFamily.getFamilyHeadId(),sysUserService.findByProperty("userId",sysFamily.getFamilyHeadId()).getUserRealName());
				}
				if(null != sysFamily.getFamilyPeopleId() && !"".equals(sysFamily.getFamilyPeopleId())){
					FamilyPeopleMap.put(sysFamily.getFamilyPeopleId(), sysUserService.findByProperty("userId",sysFamily.getFamilyPeopleId()).getUserRealName());
				}
				if(null != sysFamily.getFamilyPeopleId() && !"".equals(sysFamily.getFamilyPeopleId())){
					idCardMap.put(sysFamily.getFamilyPeopleId(), sysUserService.findByProperty("userId",sysFamily.getFamilyPeopleId()).getCardId()+"");
				}
			}
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		sysFamilyService.delete(sysFamily.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		sysFamily =  sysFamilyService.findByProperty("id",sysFamily.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		sysZuAllList = sysZuService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		sysFamilyService.add(sysFamily);
		sysFamily = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return sysFamily;
	}
	@Override
	public void prepare() throws Exception {
		if(sysFamily ==null){
			sysFamily = new SysFamily();
		}
	}


	public SysFamily getSysFamily() {
		return sysFamily;
	}

	public void setSysFamily(SysFamily sysFamily) {
		this.sysFamily = sysFamily;
	}

	public Result<SysFamily> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<SysFamily> pageResult) {
		this.pageResult = pageResult;
	}
	
	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public List<SysFamily> getSysFamilyAllList() {
		return sysFamilyAllList;
	}

	public void setSysFamilyAllList(List<SysFamily> sysFamilyAllList) {
		this.sysFamilyAllList = sysFamilyAllList;
	}

	public List<SysZu> getSysZuAllList() {
		return sysZuAllList;
	}

	public void setSysZuAllList(List<SysZu> sysZuAllList) {
		this.sysZuAllList = sysZuAllList;
	}

	

	public Map<String, String> getVillageNameMap() {
		return villageNameMap;
	}

	public void setVillageNameMap(Map<String, String> villageNameMap) {
		this.villageNameMap = villageNameMap;
	}

	


	public Map<String, String> getZuNameMap() {
		return zuNameMap;
	}

	public void setZuNameMap(Map<String, String> zuNameMap) {
		this.zuNameMap = zuNameMap;
	}

	public Map<String, String> getHuzhuNameMap() {
		return huzhuNameMap;
	}

	public void setHuzhuNameMap(Map<String, String> huzhuNameMap) {
		this.huzhuNameMap = huzhuNameMap;
	}

	public Map<String, String> getFamilyPeopleMap() {
		return FamilyPeopleMap;
	}

	public void setFamilyPeopleMap(Map<String, String> familyPeopleMap) {
		FamilyPeopleMap = familyPeopleMap;
	}

	public Map<String, String> getIdCardMap() {
		return idCardMap;
	}

	public void setIdCardMap(Map<String, String> idCardMap) {
		this.idCardMap = idCardMap;
	}

	

	public String getIdcard() {
		return Idcard;
	}

	public void setIdcard(String idcard) {
		Idcard = idcard;
	}

	public String getSexY() {
		return sexY;
	}

	public void setSexY(String sexY) {
		this.sexY = sexY;
	}

	public String getAddpeoplename() {
		return addpeoplename;
	}

	public void setAddpeoplename(String addpeoplename) {
		this.addpeoplename = addpeoplename;
	}


	

	


	

	
	
}
